package ink.xiaobaibai.controller.buy;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ink.xiaobaibai.annotation.InsidePublicResources;
import ink.xiaobaibai.bo.SelectBuyOrderBo;
import ink.xiaobaibai.common.MyRole;
import ink.xiaobaibai.entity.BuyOrder;
import ink.xiaobaibai.entity.LogisticsBuy;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IBuyOrderService;
import ink.xiaobaibai.service.ILogisticsBuyService;
import ink.xiaobaibai.units.MySecurity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @description: 购买业务订单
 * @author: 小白白
 * @create: 2021-06-15
 **/

@RestController
@RequestMapping("/buy/order")
@CrossOrigin
@Api(tags = "购买业务订单接口")
@InsidePublicResources
public class BuyOrderController {

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private IBuyOrderService buyOrderService;

    @Autowired
    private ILogisticsBuyService logisticsBuyService;

    @PostMapping("/list")
    @ApiOperation("购买业务订单集合")
    public ResponseFormat list(@RequestBody SelectBuyOrderBo selectBuyOrderBo) {
        Integer userId = MySecurity.getUserId();
        Set<String> roles = MySecurity.getRoles();
        String role = MySecurity.getRole();
        if (!roles.contains(MyRole.POWER_BUY)) {
            if (role.equals(MyRole.DISTRIBUTOR)) {
                selectBuyOrderBo.setAdminUserId(userId);
            }
            if (role.equals(MyRole.AGENT)) {
                selectBuyOrderBo.setDepId(this.adminUserService.getDepId(userId));
            }
        }
        QueryWrapper<BuyOrder> q1 = new QueryWrapper<>();
        q1.orderByDesc("create_time");
        if (selectBuyOrderBo.getOrderId() != null) {
            q1.eq("order_id", selectBuyOrderBo.getOrderId());
        }
        if (selectBuyOrderBo.getAdminUserId() != null) {
            q1.eq("admin_user_id", selectBuyOrderBo.getAdminUserId());
        }
        if (selectBuyOrderBo.getClientUserId() != null) {
            q1.eq("client_user_id", selectBuyOrderBo.getClientUserId());
        }
        if (selectBuyOrderBo.getDepId() != null) {
            q1.eq("admin_dep_id", selectBuyOrderBo.getDepId());
        }
        if (selectBuyOrderBo.getDeviceCode() != null) {
            q1.eq("device_code", selectBuyOrderBo.getDeviceCode());
        }
        if (selectBuyOrderBo.getDeviceId() != null) {
            q1.eq("device_id", selectBuyOrderBo.getDeviceId());
        }
        if (selectBuyOrderBo.getStatus() != null) {
            q1.eq("status", selectBuyOrderBo.getStatus());
        }
        Page<BuyOrder> page = this.buyOrderService.page(new Page<>(selectBuyOrderBo.getPageNum(), selectBuyOrderBo.getPageSize()), q1);
        return ResponseFormat.success(page);
    }

    @GetMapping("/listByClientId")
    @ApiOperation("根据用户Id查看订单")
    public ResponseFormat getListByClientId(@RequestParam("clientId") Integer clientId,
                                            @RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("pageSize") Integer pageSize) {
        IPage<BuyOrder> page = new Page<>(pageNum, pageSize);
        QueryWrapper<BuyOrder> q1 = new QueryWrapper<>();
        q1.eq("client_user_id", clientId);
        IPage<BuyOrder> result = this.buyOrderService.page(page, q1);
        return ResponseFormat.success(result);
    }

    @GetMapping("/item")
    @ApiOperation("单个订单详细")
    public ResponseFormat item(@RequestParam("orderId") String orderId) {
        Integer userId = MySecurity.getUserId();
        Set<String> roles = MySecurity.getRoles();
        String role = MySecurity.getRole();
        BuyOrder order = this.buyOrderService.getById(orderId);
        if (!roles.contains(MyRole.POWER_BUY)) {
            if (role.equals(MyRole.DISTRIBUTOR) && !order.getAdminUserId().equals(userId)) {
                return ResponseFormat.fail("无权查看他人订单");
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("order", order);
        jsonObject.put("logistics", this.logisticsBuyService.getById(order.getLogisticsBuyId()));
        return ResponseFormat.success(jsonObject);
    }

    @GetMapping("/logistics/{id}")
    @ApiOperation("根据物流id查看物流个人信息")
    public ResponseFormat logisticsById(@PathVariable Integer id) {
        LogisticsBuy logisticsBuy = this.logisticsBuyService.getById(id);
        return ResponseFormat.success(logisticsBuy);
    }


}
